package ex.strategy.spinup_chained;
import java.util.Arrays;
/**
 * Created with IntelliJ IDEA.
 * User: sababa
 * Date: 08.02.13
 * Time: 20:47
 * To change this template use File | Settings | File Templates.
 */
public class ExceptionLab_ChainedExceptions {
    public static void main(String[] args) {
        try {
            f();
        } catch (RuntimeException e) {
            String[] messageArray = extractMessages(e);
            System.out.println(Arrays.toString(messageArray));
        }
    }

    public static void f() {
        RuntimeException tail = new RuntimeException("0");
        for (int k = 1; k < 10; k++) {
            tail = new RuntimeException("" + k, tail);
        }
        throw tail;
    }

    public static String[] extractMessages(Throwable t) {
        String[] messageArray = new String[10];
        int k = 0;
        while (t.getCause() != null) {
            messageArray[k++] = t.getMessage();
            t = t.getCause();
        }
        messageArray[k] = t.getMessage();
        return messageArray;
    }
}
